const {readBody} = require('./utils');

function htmlRewritePlugin({root,app}){
  const inject = `
    <script>
        window.process = {};
        process.env = {NODE_ENV:'development'}
    </script>
  `;

  //todo 这里还可以注入热更新脚本
  app.use(async (ctx, next) => {
    await next();
    if (ctx.response.is('html')) {
      const html = await readBody(ctx.body);

      ctx.body = html.replace(/<head>/, `$&${inject}`/*←←$&能在被匹配到到的字符串的后面追加内容*/);
    }
  });
}

exports.htmlRewritePlugin = htmlRewritePlugin;
